import { PcSeriveProvider } from "./../../../providers/pc-serive/pc-serive";
import { Component, ViewChild, } from "@angular/core";
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController,
  Navbar,
} from "ionic-angular";
import { BaseUI } from "../../../common/baseUI";

/**
 * Generated class for the PcRegistrationEvaluationPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-pc-registration-evaluation",
  templateUrl: "pc-registration-evaluation.html"
})
export class PcRegistrationEvaluationPage extends BaseUI {
  @ViewChild(Navbar) navBar: Navbar;
  name: string = "";//项目名称
  value = '';//最大分值
  values = '';//最大分值
  diff: number = 0;//高低分值
  sort: number = 0;//排序分值
  itemId;//判断用的
  animation:boolean = false//判断动画用的
  //保存输入的值
  item = {
    name: "",
    value: 0,
    score: {
      diff: 0,
      sort: 0,
    }
  };
  callback;
  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    private toast: ToastController,
    private pcSerive: PcSeriveProvider,
  ) {
    super();
    this.callback = this.navParams.get("callback");
    let id = this.navParams.get("id");//小项编号
    //判断是否有传id进来，有就做复制操作
    if (!isNaN(id)) {
      this.itemId = id;//单项id
      this.name = this.pcSerive.items[id]['name'];//单项名称
      this.value = this.pcSerive.items[id]['value'];//单项分数
      this.diff = this.pcSerive.items[id]['score']['diff'];//高低分差
      this.sort = this.pcSerive.items[id]['score']['sort'];//排序比分
    };
  }

  //更改左上角返回触发的操作
  ionViewDidLoad() {
    this.navBar.backButtonClick = this.backButtonClick;
  }
  backButtonClick = (e: UIEvent) => {
    this.navCtrl.pop();
  };

  rep($event) {
    //删除键
    if ($event.keyCode == 8 && this.value.length == 0) {
      this.value = this.values = '';
      return;
    }
    //通过value保存值的改变 来判断用户是否输入非法字符
    //输入非法字符后input的值会突然为空
    if (this.value != '') {
      this.values = this.value;
    } else {
      this.value = this.values == '' ? '0 ' : this.values;
    }
    this.value = this.value.replace(/[eE]/, '');
    //限制输入长度
    if (this.value.length > 3) this.value = this.value.slice(0, 3);
  }
  isEmoji($event) {
    //判断是不是emoji表情，我就不给你输入emoji表情
    this.name = this.name.replace(/[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF][\u200D|\uFE0F]|[\uD83C|\uD83D|\uD83E][\uDC00-\uDFFF]|[0-9|*|#]\uFE0F\u20E3|[0-9|#]\u20E3|[\u203C-\u3299]\uFE0F\u200D|[\u203C-\u3299]\uFE0F|[\u2122-\u2B55]|\u303D|[\A9|\AE]\u3030|\uA9|\uAE|\u3030/ig, '');

    //限制输入长度
    if (this.name.length > 12) this.name = this.name.slice(0, 12);
  }

  returnCreater() {
    if (!this.name) {
      this.showToast(this.toast, "你没有输入项目名称");
    } else if (this.name.length > 12) {
      this.showToast(this.toast, "项目名称过长，最大12个字");
    } else if (this.value == '' || !/^\d+$/.test(this.value)) {
      this.showToast(this.toast, '输入有误,请输入正确数值'); 
    } else if (this.value.length > 3) {
      this.showToast(this.toast, '输入有误,最大只能输入3位数');
    } else {

      if (this.sort > parseInt(this.value)) this.sort = parseInt(this.value);//如果当前选中的排序分差大过最大分值，排序分差等于最大分值
      if (this.diff > parseInt(this.value)) this.diff = parseInt(this.value);//如果当前选中的高低分差大过最大分值，高低分差等于最大分值

      this.item['score']['diff'] = this.diff;
      this.item['score']['sort'] = this.sort;
      this.item.name = this.name;
      this.item.value = parseInt(this.value.toString());

      //做判断，判断是更新操作还是新建操作
      if (this.itemId || this.itemId == 0) {
        this.pcSerive.items[this.itemId]['name'] = this.item['name'];
        this.pcSerive.items[this.itemId]['value'] = this.item['value'];
        this.pcSerive.items[this.itemId]['score'] = this.item['score'];
        this.navCtrl.pop();
      } else {
        this.pcSerive.items.push(this.item);
        this.navCtrl.pop();
      }

    }
  }

  openmore(){
    this.animation = !this.animation ;
  }
}
